package cz.possoft.client.piskvorky.gui.components;

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import cz.possoft.client.piskvorky.gui.panel.PiskvorkyPanel;
import cz.possoft.client.utils.PanelHolderUtils;
import cz.possoft.serializable.generic.Location;

/**
  * Listener nastava pri pohybu mysi nad sachovnici hry piskvorky
  *
  * @author mara  
  */
public class PiskvorkyMouseMotionListener extends MouseMotionAdapter {
	 
	    public void mouseMoved(MouseEvent me) {
	       	if (!(PanelHolderUtils.getCurrentGamePanel() instanceof PiskvorkyPanel)) {
	    		return;
	    	}    	    	
	        if ((PanelHolderUtils.getCurrentGamePanel().getGame().isActive()) && (PanelHolderUtils.getCurrentGamePanel().getGame().jsemNaTahu())) {
	            Location location = getMouseLocation(me);  
	            ((PiskvorkyPanel)PanelHolderUtils.getCurrentGamePanel()).updateForPlaceWhereIsMouse(location);	            
	        }        
	    }

	    // vraci umisteni kde se prave nachazi mys ([0..7, 0..7])
	    // pro y-souradnici vraci vzdy horni radu.
	    public static Location getMouseLocation(MouseEvent me) {
	        int xx = me.getX();
	        int yy = me.getY();
	        if ((xx < 8) || (xx > 441) || (yy < 8) || (yy > 441)) {
	            return new Location(-1, -1);
	        } else {        	
	        	int xCil = (xx - 8) / PiskvorkyPane.ROZMER_CTVERCE;
	        	// oznacena je vzdy horni rada
	        	int yCil = (yy - 8) / PiskvorkyPane.ROZMER_CTVERCE;;	        	     	
	        	// navrat lokace
	            return new Location(xCil, yCil);
	        }
	    }	
	    
}
